﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc.Models;
using System.Reflection;

namespace Mvc.Models
{
    public class PermissionsController : Controller
    {
        private UsersContext db = new UsersContext();

        //
        // GET: /Permmisions/

        public ActionResult Index(int GroupId)
        {
            
            var controllers = Assembly.GetExecutingAssembly().GetExportedTypes().Where(t => typeof(ControllerBase).IsAssignableFrom(t)).Select(t => t);
            foreach (Type controller in controllers)
            {
                var actions = controller.GetMethods().Where(t => t.Name != "Dispose" && !t.IsSpecialName && t.DeclaringType.IsSubclassOf(typeof(ControllerBase)) && t.IsPublic && !t.IsStatic).ToList();
                ;
                foreach (var action in actions)
                {
                    var myAttributes = action.GetCustomAttributes(false);
                    for (int j = 0; j < myAttributes.Length; j++) {

                       

                        var permiso = from q in db.Permissions
                                       where (q.GroupId == GroupId) &&
                                       (q.Controlador.ToUpper() == controller.Name) &&
                                       (q.Accion.ToUpper() == action.Name)
                                       select q;

                        if (permiso.Count()==0) {

                            Permission p = new Permission();
                            p.GroupId = GroupId;
                            p.Accion = action.Name;
                            p.Controlador = controller.Name;
                            p.Concedido = false;
                            db.Permissions.Add(p);
                            db.SaveChanges();

                        }
                    }


                }
            }
            var permisos = from p in db.Permissions
                           where p.GroupId == GroupId
                           orderby p.Controlador ascending,p.Accion ascending
                           select p;
            
            
            return PartialView(permisos.ToList());
        }

    
       

        //
        // GET: /Permmisions/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Permission permission = db.Permissions.Find(id);

            if (permission == null)
            {
                return HttpNotFound();
            }
            else {
                
                if (permission.Concedido==true)
                {
                    permission.Concedido = false;
                }
                else {

                    permission.Concedido = true;
                }

                db.Entry(permission).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { GroupId =permission.GroupId});
            }
           
        }

        public ActionResult Create() {

            ViewBag.GroupId = new SelectList(db.Groups, "GroupId", "Nombre");
            return PartialView();

        }


        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Create(Permission permiso)
        {
            if (ModelState.IsValid)
            {
                db.Permissions.Add(permiso);
                db.SaveChanges();
                return RedirectToAction("Index", new { GroupId=permiso.GroupId});
            }
            ViewBag.GroupId = new SelectList(db.Groups, "GroupId", "Nombre");
            return PartialView();
        }

        public ActionResult modify(int id) {
            Permission permiso = db.Permissions.Find(id);
          
            return PartialView(permiso);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult modify(Permission permiso)
        {
            if (ModelState.IsValid)
            {
                db.Entry(permiso).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { GroupId=permiso.GroupId});
            }
            return PartialView(permiso);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }



    }
}